﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace Secams.utils
{
    class MdbUtil
    {
        public static OleDbConnection getConnection(string mdbFilepath)
        {
            string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+mdbFilepath+";";
            OleDbConnection oConn = new OleDbConnection();
            oConn.ConnectionString = strAccessConn;
            oConn.Open();

            return oConn;
        }

        public static void create(string mdbFilepath)
        {
            /**
            ADOX.CatalogClass cat = new CatalogClass();
                string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "DataBase\\" + filename + ".mdb;";
                cat.Create(str);
                cat = null;
             */

        }

        public static OleDbParameter createDbParameter(string paramName, object paramVal)
        {
            OleDbParameter insertParameter = new OleDbParameter();
            insertParameter.ParameterName = paramName;
            insertParameter.Value = paramVal;
            return insertParameter;
        }

        public static bool existsTable(OleDbConnection dbConn, String tableName)
        {
            bool retval = false;
            DataTable schemaTable = dbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                    new Object[] { null, null, null, "TABLE" });

            for (int i = 0; i < schemaTable.Rows.Count; i++)
            {
                if (schemaTable.Rows[i].ItemArray[2].ToString().Equals(tableName))
                {
                    retval = true;
                    break;
                }
            }
            return retval;
        }

    }
}
